

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta name="referrer" content="no-referrer" />
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/apple-touch-icon.png">
  <link rel="icon" href="/img/fyy.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="fyy">
  <meta name="keywords" content="">
  
    <meta name="description" content="预备知识python语言基础 目标导入图片数据集，分析图片的特点、定义变量，构建模型，训练模型并输出中间状态参数，测试、保存、读取模型 如何搞定它1.1导入图片数据集首先来看看数据集是什么样的。MNIST是一个入门级的计算机视觉数据集。当我们开始学习编程时，第一件事往往是学习打印Hello World。在机器学习入门的领域里，我们会用MNIST数据集来实验各种模型。 1.1.1数据集介绍 MNIS">
<meta property="og:type" content="article">
<meta property="og:title" content="识别图中模糊的手写数字">
<meta property="og:url" content="http://example.com/2020/11/29/%E8%AF%BE%E7%A8%8B%E5%AE%9E%E9%AA%8C/%E8%AF%86%E5%88%AB%E5%9B%BE%E4%B8%AD%E6%A8%A1%E7%B3%8A%E7%9A%84%E6%89%8B%E5%86%99%E6%95%B0%E5%AD%97/index.html">
<meta property="og:site_name" content="fyy-coding">
<meta property="og:description" content="预备知识python语言基础 目标导入图片数据集，分析图片的特点、定义变量，构建模型，训练模型并输出中间状态参数，测试、保存、读取模型 如何搞定它1.1导入图片数据集首先来看看数据集是什么样的。MNIST是一个入门级的计算机视觉数据集。当我们开始学习编程时，第一件事往往是学习打印Hello World。在机器学习入门的领域里，我们会用MNIST数据集来实验各种模型。 1.1.1数据集介绍 MNIS">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/fuyingyou/picgo/raw/master/img_algorithm/202403161546895.png">
<meta property="og:image" content="https://gitee.com/fuyingyou/picgo/raw/master/img_algorithm/202403161546896.png">
<meta property="og:image" content="https://gitee.com/fuyingyou/picgo/raw/master/img_algorithm/202403161546897.png">
<meta property="og:image" content="https://gitee.com/fuyingyou/picgo/raw/master/img_algorithm/202403161546898.png">
<meta property="og:image" content="https://gitee.com/fuyingyou/picgo/raw/master/img_algorithm/202403161546899.png">
<meta property="og:image" content="https://gitee.com/fuyingyou/picgo/raw/master/img_algorithm/202403161546900.png">
<meta property="article:published_time" content="2020-11-29T06:59:44.000Z">
<meta property="article:modified_time" content="2024-03-18T14:32:16.171Z">
<meta property="article:author" content="fyy">
<meta property="article:tag" content="神经网络">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://gitee.com/fuyingyou/picgo/raw/master/img_algorithm/202403161546895.png">
  
  
  
  <title>识别图中模糊的手写数字 - fyy-coding</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.7","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"follow_dnt":true,"baidu":null,"google":{"measurement_id":null},"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":"ufph8TbmK43d1JfQxLvss4KY-MdYXbMMI","app_key":"96APiPOtZEiCQlv5h3jjZfbC","server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml","include_content_in_search":true};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  

  

  
    <!-- Google tag (gtag.js) -->
    <script async>
      if (!Fluid.ctx.dnt) {
        Fluid.utils.createScript("https://www.googletagmanager.com/gtag/js?id=", function() {
          window.dataLayer = window.dataLayer || [];
          function gtag() {
            dataLayer.push(arguments);
          }
          gtag('js', new Date());
          gtag('config', '');
        });
      }
    </script>
  

  

  

  

  
    
  



  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>fyy-coding</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/" target="_self">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/" target="_self">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/" target="_self">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/" target="_self">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/" target="_self">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="识别图中模糊的手写数字"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2020-11-29 14:59" pubdate>
          2020年11月29日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          4.1k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          35 分钟
        
      </span>
    

    
    
      
        <span id="leancloud-page-views-container" class="post-meta" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="leancloud-page-views"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <h1 id="seo-header">识别图中模糊的手写数字</h1>
            
            
              <div class="markdown-body">
                
                <h4 id="预备知识"><a href="#预备知识" class="headerlink" title="预备知识"></a>预备知识</h4><p>python语言基础</p>
<h4 id="目标"><a href="#目标" class="headerlink" title="目标"></a>目标</h4><p>导入图片数据集，分析图片的特点、定义变量，构建模型，训练模型并输出中间状态参数，测试、保存、读取模型</p>
<h4 id="如何搞定它"><a href="#如何搞定它" class="headerlink" title="如何搞定它"></a>如何搞定它</h4><h5 id="1-1导入图片数据集"><a href="#1-1导入图片数据集" class="headerlink" title="1.1导入图片数据集"></a>1.1导入图片数据集</h5><p>首先来看看数据集是什么样的。<br>MNIST是一个入门级的计算机视觉数据集。当我们开始学习编程时，第一件事往往是学习打印Hello World。在机器学习入门的领域里，我们会用MNIST数据集来实验各种模型。</p>
<p>1.1.1数据集介绍</p>
<p>MNIST里包含各种手写数字图片，如图所示。<br><img src="https://gitee.com/fuyingyou/picgo/raw/master/img_algorithm/202403161546895.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>它也包含每一张图片对应的标签，告诉我们这个是数字几。例如，上面这4张图片的标签分别是5、0、4、1。</p>
<p>1.1.2下载并安装MNIST数据集</p>
<p>介绍完MNIST数据集后，下面来演示一下如何通过代码来对其操作。</p>
<p>（1）利用TensorFlow代码下载MNIST</p>
<p>TensorFlow提供了一个库，可以直接用来自动下载与安装MNIST，见如下代码：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs js"># <span class="hljs-variable constant_">MNIST</span>数据集<br><span class="hljs-keyword">from</span> tensorflow.<span class="hljs-property">examples</span>.<span class="hljs-property">tutorials</span>.<span class="hljs-property">mnist</span> <span class="hljs-keyword">import</span> input_data<br>mnist=input_data.<span class="hljs-title function_">read_data_sets</span>(<span class="hljs-string">&quot;MNIST_data/&quot;</span>,one_hot=<span class="hljs-title class_">True</span>))<br></code></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs js">运行上面的代码，会自动下载数据集并将文件解压到当前代码所在同级目录下的MNIST_data文件夹下。<br>注意：代码中的one_hot=<span class="hljs-title class_">True</span>，表示将样本标签转化为one_hot编码。<br></code></pre></td></tr></table></figure>

<p>举例来解释one_hot编码：<br>假如一共10类。0的one_hot为1000000000，1的one_hot为0100000000，2的one_hot为0010000000，3的one_hot为0001000000……依此类推。只有一个位为1，1所在的位置就代表着第几类。</p>
<p>MNIST数据集中的图片是28×28像素，所以，每一幅图就是1行784（28×28）列的数据，括号中的每一个值代表一个像素。</p>
<ul>
<li>如果是黑白的图片，图片中黑色的地方数值为0；有图案的地方，数值为0～255之间的数字，代表其颜色的深度。</li>
<li>如果是彩色的图片，一个像素会由3个值来表示RGB（红、黄、蓝）。在后面讲解其他数据集时会具体讲到。</li>
</ul>
<p>接下来通过几行代码将MNIST里面的信息打印出来，看看它的具体内容。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs js"># <span class="hljs-variable constant_">MNIST</span>数据集（续）<br>print (<span class="hljs-string">&#x27;输入数据:&#x27;</span>,mnist.<span class="hljs-property">train</span>.<span class="hljs-property">images</span>)<br>print (<span class="hljs-string">&#x27;输入数据打印shape:&#x27;</span>,mnist.<span class="hljs-property">train</span>.<span class="hljs-property">images</span>.<span class="hljs-property">shape</span>)<br><span class="hljs-keyword">import</span> pylab<br>im = mnist.<span class="hljs-property">train</span>.<span class="hljs-property">images</span>[<span class="hljs-number">1</span>]<br>im = im.<span class="hljs-title function_">reshape</span>(-<span class="hljs-number">1</span>,<span class="hljs-number">28</span>)<br>pylab.<span class="hljs-title function_">imshow</span>(im)<br>pylab.<span class="hljs-title function_">show</span>()<br></code></pre></td></tr></table></figure>

<p>运行上面的代码，输出信息如下：</p>
<p>输出结果如图所示<br><img src="https://gitee.com/fuyingyou/picgo/raw/master/img_algorithm/202403161546896.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>刚开始的打印信息是解压数据集的意思。如果是第一次运行，还会显示下载数据的相关信息。<br>接着打印出来的是训练集的图片信息，是一个55000行、784列的矩阵。即，训练集里有55000张图片。</p>
<p>（2）MNIST数据集组成</p>
<p>在MNIST训练数据集中，mnist.train.images是一个形状为[55000，784]的张量。其中，第1个维度数字用来索引图片，第2个维度数字用来索引每张图片中的像素点。此张量里的每一个元素，都表示某张图片里的某个像素的强度值，值介于0～255之间。<br>MNIST里包含3个数据集：第一个是训练数据集，另外两个分别是测试数据集（mnist.test）和验证数据集（mnist.validation）。可使用如下命令查看里面的数据信息：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-variable constant_">MNIST</span>数据集（续）<br>print (<span class="hljs-string">&#x27;输入数据打印shape:&#x27;</span>,mnist.<span class="hljs-property">test</span>.<span class="hljs-property">images</span>.<span class="hljs-property">shape</span>)<br>print (<span class="hljs-string">&#x27;输入数据打印shape:&#x27;</span>,mnist.<span class="hljs-property">validation</span>.<span class="hljs-property">images</span>.<span class="hljs-property">shape</span>)<br></code></pre></td></tr></table></figure>

<p>运行完上面的命令，可以发现在测试数据集里有10000条样本图片，验证数据集里有5000个图片。</p>
<p>在实际的机器学习模型设计时，样本一般分为3部分：</p>
<ul>
<li>一部分用于训练；</li>
<li>一部分用于评估训练过程中的准确度（测试数据集）；</li>
<li>一部分用于评估最终模型的准确度（验证数据集）。</li>
</ul>
<p>训练过程中，模型并没有遇到过验证数据集中的数据，所以利用验证数据集可以评估出模型的准确度。这个准确度越高，代表模型的泛化能力越强。</p>
<p>另外，这3个数据集还有分别对应的3个文件（标签文件），用来标注每个图片上的数字是几。把图片和标签放在一起，称为“样本”。通过样本来就可以实现一个有监督信号的深度学习模型。</p>
<p>相对应的，MNIST数据集的标签是介于0～9之间的数字，用来描述给定图片里表示的数字。标签数据是“one-hot vectors”：一个one-hot向量，除了某一位的数字是1外，其余各维度数字都是0。例如，标签0将表示为（[1，0，0，0，0，0，0，0，0，0，0]）。因此，mnist.train.labels是一个[55000，10]的数字矩阵。</p>
<h5 id="1-2分析图片的特点，定义变量"><a href="#1-2分析图片的特点，定义变量" class="headerlink" title="1.2分析图片的特点，定义变量"></a>1.2分析图片的特点，定义变量</h5><p>由于输入图片是个55000×784的矩阵，所以先创建一个[None，784]的占位符x和一个[None，10]的占位符y，然后使用feed机制将图片和标签输入进去。具体代码如下。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs js"># <span class="hljs-variable constant_">MNIST</span>分类<br><span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf  # 导入tensorflow库<br><span class="hljs-keyword">from</span> tensorflow.<span class="hljs-property">examples</span>.<span class="hljs-property">tutorials</span>.<span class="hljs-property">mnist</span> <span class="hljs-keyword">import</span> input_data<br>mnist = input_data.<span class="hljs-title function_">read_data_sets</span>(<span class="hljs-string">&quot;MNIST_data/&quot;</span>,one_hot=<span class="hljs-title class_">True</span>)<br><span class="hljs-keyword">import</span> pylab <br>tf.<span class="hljs-title function_">reset_default_graph</span>()<br># 定义占位符<br>x = tf.<span class="hljs-title function_">placeholder</span>(tf.<span class="hljs-property">float32</span>, [<span class="hljs-title class_">None</span>, <span class="hljs-number">784</span>]) # <span class="hljs-variable constant_">MNIST</span>数据集的维度是  <span class="hljs-number">28</span>×<span class="hljs-number">28</span>=<span class="hljs-number">784</span><br>y = tf.<span class="hljs-title function_">placeholder</span>(tf.<span class="hljs-property">float32</span>, [<span class="hljs-title class_">None</span>, <span class="hljs-number">10</span>])  # 数字<span class="hljs-number">0</span>～<span class="hljs-number">9</span> ，共<span class="hljs-number">10</span>个类别<br>#代码中第<span class="hljs-number">8</span>行的<span class="hljs-title class_">None</span>，表示此张量的第一个维度可以是任何长度的。x就代表能够输入任意数量的<span class="hljs-variable constant_">MNIST</span>图像，每一张图展平成<span class="hljs-number">784</span>维的向量。<br></code></pre></td></tr></table></figure>

<h5 id="1-3构建模型"><a href="#1-3构建模型" class="headerlink" title="1.3构建模型"></a>1.3构建模型</h5><p>样本完成后就可以构建模型了。下面列出了构建模型的相关步骤。</p>
<p>1.3.1　定义学习参数</p>
<p>模型也需要权重值和偏置量，它们被统一叫做学习参数。在TensorFlow里，使用Variable来定义学习参数。<br>一个Variable代表一个可修改的张量，定义在TensorFlow的图（一个执行任务）中，其本身也是一种变量。使用Variable定义的学习参数可以用于计算输入值，也可以在计算中被修改。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs js"># <span class="hljs-variable constant_">MNIST</span>分类（续）<br>W = tf.<span class="hljs-title class_">Variable</span>(tf.<span class="hljs-title function_">random_normal</span>(([<span class="hljs-number">784</span>,<span class="hljs-number">10</span>]))<br>b = tf.<span class="hljs-title class_">Variable</span>(tf.<span class="hljs-title function_">zeros</span>([<span class="hljs-number">10</span>]))<br></code></pre></td></tr></table></figure>

<p>在这里赋予tf.Variable不同的初值来创建不同的参数。一般将W设为一个随机值，将b设为0。<br>注意：W的维度是[784，10]，因为想要用784维的图片向量乘以它，以得到一个10维的证据值向量，每一位对应不同数字类。b的形状是[10]，所以可以直接把它加到输出上面。</p>
<p>1.3.2　定义输出节点</p>
<p>有了输入和模型参数，接着便可以将它们串起来构建成真正的模型。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs js"># <span class="hljs-variable constant_">MNIST</span>分类（续）<br>pred = tf.<span class="hljs-property">nn</span>.<span class="hljs-title function_">softmax</span>(tf.<span class="hljs-title function_">matmul</span>(x, W) + b) # <span class="hljs-title class_">Softmax</span>分类<br></code></pre></td></tr></table></figure>

<p>首先，用tf.matmul（x，W）表示x乘以W，这里x是一个二维张量，拥有多个输入。然后再加上b，把它们的和输入到tf.nn.softmax函数里。<br>至此就构建好了正向传播的结构。也就是表明，只要模型中的参数合适，通过具体的数据输入，就能得到我们想要的分类。</p>
<p>1.3.3　定义反向传播的结构</p>
<p>下面定义一个反向传播的结构，编译训练模型，以得到合适的参数。<br>这里涉及一个“学习率”的概念。学习率，是指每次改变学习参数的大小。在这里读者只要先有个概念即可，后面章节还会详细介绍。<br>先看下面代码。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs js">代码<span class="hljs-number">1</span>-<span class="hljs-number">2</span>　<span class="hljs-variable constant_">MNIST</span>分类（续）<br># 损失函数<br>cost=tf.<span class="hljs-title function_">reduce_mean</span>(-tf.<span class="hljs-title function_">reduce_sum</span>(y*tf.<span class="hljs-title function_">log</span>(pred),reduction_indices=<span class="hljs-number">1</span>))<br>  <br># 定义参数<br>learning_rate = <span class="hljs-number">0.01</span><br># 使用梯度下降优化器<br>optimizer=tf.<span class="hljs-property">train</span>.<span class="hljs-title class_">GradientDescentOptimizer</span>(learning_rate).<span class="hljs-title function_">minimize</span>(cost)<br></code></pre></td></tr></table></figure>

<p>上面的代码可以这样来理解：<br>（1）将生成的pred与样本标签y进行一次交叉熵的运算，然后取平均值。<br>（2）将这个结果作为一次正向传播的误差，通过梯度下降的优化方法找到能够使这个误差最小化的b和W的偏移量。<br>（3）更新b和W，使其调整为合适的参数。<br>整个过程就是不断地让损失值（误差值cost）变小。因为损失值越小，才能表明输出的结果跟标签数据越相近。当cost小到我们的需求时，这时的b和W就是训练出来的合适值。</p>
<h5 id="1-4-训练模型并输出中间状态参数"><a href="#1-4-训练模型并输出中间状态参数" class="headerlink" title="1.4　训练模型并输出中间状态参数"></a>1.4　训练模型并输出中间状态参数</h5><p>现在开始真正地训练模型了，先定义训练相关的参数。<br>下面代码中</p>
<ul>
<li>第1行中，training_epochs代表要把整个训练样本集迭代25次；</li>
<li>第2行中，batch_size代表在训练过程中一次取100条数据进行训练</li>
<li>第3行中，display_step代表每训练一次就把具体的中间状态显示出来。</li>
</ul>
<p>注意：batch_size参数代表的意义很关键，在深度学习中，都是将数据按批次地向里面放的。在后面章节中还会详细介绍这么做的目的。<br>参数定义好后，启动一个session就可以开始训练过程了。session中有两个run，第一个run是运行初始化，第二个run是运行具体的运算模型。模型运算之后便将里面的状态打印出来。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs js">training_epochs = <span class="hljs-number">25</span><br>batch_size = <span class="hljs-number">100</span><br>display_step = <span class="hljs-number">1</span><br><br>saver = tf.<span class="hljs-property">train</span>.<span class="hljs-title class_">Saver</span>()<br>model_path = <span class="hljs-string">&quot;log/521model.ckpt&quot;</span><br><br># 启动session<br><span class="hljs-keyword">with</span> tf.<span class="hljs-title class_">Session</span>() <span class="hljs-keyword">as</span> <span class="hljs-attr">sess</span>:<br>    sess.<span class="hljs-title function_">run</span>(tf.<span class="hljs-title function_">global_variables_initializer</span>())# <span class="hljs-title class_">Initializing</span> <span class="hljs-variable constant_">OP</span><br>    # 启动循环开始训练<br>    <span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-title function_">range</span>(training_epochs):<br>        avg_cost = <span class="hljs-number">0.</span><br>        total_batch = <span class="hljs-title function_">int</span>(mnist.<span class="hljs-property">train</span>.<span class="hljs-property">num_examples</span>/batch_size)<br>        # 循环所有数据集<br>        <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-title function_">range</span>(total_batch):<br>            batch_xs, batch_ys = mnist.<span class="hljs-property">train</span>.<span class="hljs-title function_">next_batch</span>(batch_size)<br>            # 运行优化器<br>            _, c = sess.<span class="hljs-title function_">run</span>([optimizer, cost], feed_dict=&#123;<br>    <span class="hljs-attr">x</span>: batch_xs,<br>                                                       <span class="hljs-attr">y</span>: batch_ys&#125;)<br>            # 计算平均loss值<br>            avg_cost += c / total_batch<br>        # 显示训练中的详细信息<br>        <span class="hljs-keyword">if</span> (epoch+<span class="hljs-number">1</span>) % display_step == <span class="hljs-number">0</span>:<br>           print (<span class="hljs-string">&quot;Epoch:&quot;</span>, <span class="hljs-string">&#x27;%04d&#x27;</span> % (epoch+<span class="hljs-number">1</span>), <span class="hljs-string">&quot;cost=&quot;</span>, <span class="hljs-string">&quot;&#123;:.9f&#125;&quot;</span>.            <span class="hljs-title function_">format</span>(avg_cost))<br><br>    <span class="hljs-title function_">print</span>( <span class="hljs-string">&quot; Finished!&quot;</span>)<br></code></pre></td></tr></table></figure>

<p>执行上面的代码，会输出如下信息：<br><img src="https://gitee.com/fuyingyou/picgo/raw/master/img_algorithm/202403161546897.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>这里输出的中间状态是cost损失值。读者也可以把自己关心的内容打印出来。可以看到，从第1次迭代到第25次迭代的损失值在逐渐减小，最终的误差只有0.8。</p>
<h5 id="1-5-测试模型"><a href="#1-5-测试模型" class="headerlink" title="1.5　测试模型"></a>1.5　测试模型</h5><p>还记得MNIST里面有测试数据吗？现在我们使用测试数据来测试一下训练完的模型吧。<br>与前面的过程类似，也是先将计算测试的网络结构建立起来，然后通过最终节点的eval将测试值运算出来。<br>注意：这个过程仍然是在session里进行的。<br>测试错误率的算法是：直接判断预测的结果与真实的标签是否相同，如是相同的就表明是正确的，如是不相同的就表示是错误的。然后将正确的个数除以总个数，得到的值即为正确率。由于是onehot编码，这里使用了tf.argmax函数返回onehot编码中数值为1的那个元素的下标。下面是具体代码。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs js">#<span class="hljs-variable constant_">MNIST</span>分类（续）<br>    correct_prediction = tf.<span class="hljs-title function_">equal</span>(tf.<span class="hljs-title function_">argmax</span>(pred, <span class="hljs-number">1</span>), tf.<span class="hljs-title function_">argmax</span>(y, <span class="hljs-number">1</span>))<br>    # 计算准确率<br>    accuracy = tf.<span class="hljs-title function_">reduce_mean</span>(tf.<span class="hljs-title function_">cast</span>(correct_prediction, tf.<span class="hljs-property">float32</span>))<br>    print (<span class="hljs-string">&quot;Accuracy:&quot;</span>, accuracy.<span class="hljs-built_in">eval</span>(&#123;<br>    <span class="hljs-attr">x</span>: mnist.<span class="hljs-property">test</span>.<span class="hljs-property">images</span>, <span class="hljs-attr">y</span>: mnist.<span class="hljs-property">test</span>.<span class="hljs-property">labels</span>&#125;))<br></code></pre></td></tr></table></figure>

<p>上面代码执行后，显示信息如下：<br><img src="https://gitee.com/fuyingyou/picgo/raw/master/img_algorithm/202403161546898.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>测试正确率的算法与损失值的算法略有差别，但代表的意义却很类似。当然，也可以直接拿计算损失值的交叉熵结果来代表模型测试的错误率。<br>注意：<br>（1）并不是所有模型的测试错误率和训练时的最后一次损失值都很接近，这取决于训练样本和测试样本的分布情况，也取决于模型本身的拟合质量。关于拟合质量问题，将在后面章节详细介绍。<br>（2）读者自己运行时，得到的值可能和本书中的值不一样。甚至每次运行时，得到的值也不一样。原因是每次初始的权重w都是随机的。由于初始权重不同，而且每次训练的批次数据也不同，所以最终生成的模型也不会完全相同。但如果核心算法保持一致，则会保证最终的结果不会有太大的偏差。</p>
<h5 id="1-6-保存模型"><a href="#1-6-保存模型" class="headerlink" title="1.6　保存模型"></a>1.6　保存模型</h5><p>下面开始讲解如何保存模型。<br>首先要建立一个saver和一个路径，然后通过调用save，自动将session中的参数保存起来，见如下代码。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs js"># <span class="hljs-variable constant_">MNIST</span>分类（续）   <br>	# 保存模型<br>    save_path = saver.<span class="hljs-title function_">save</span>(sess, model_path)<br>    <span class="hljs-title function_">print</span>(<span class="hljs-string">&quot;Model saved in file: %s&quot;</span> % save_path)<br></code></pre></td></tr></table></figure>

<p>上面代码的作用是保存模型，并将模型保存的路径打印出来。当然，在这段代码运行之前，需要添加saver和model_path的定义。来到前面session创建之前添加如下代码：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs js"># <span class="hljs-variable constant_">MNIST</span>分类（续）<br>saver = tf.<span class="hljs-property">train</span>.<span class="hljs-title class_">Saver</span>()<br>model_path = <span class="hljs-string">&quot;log/521model.ckpt&quot;</span><br></code></pre></td></tr></table></figure>

<p>执行上述的全部代码后，会打印出存储位置<br><img src="https://gitee.com/fuyingyou/picgo/raw/master/img_algorithm/202403161546899.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h5 id="1-7-读取模型"><a href="#1-7-读取模型" class="headerlink" title="1.7　读取模型"></a>1.7　读取模型</h5><p>将模型存储好后，下面来做一个实验：读取模型并将两张图片放进去让模型预测结果，然后将两张图片极其对应的标签一并显示出来。<br>在整个代码执行过程中，对于网络模型的定义不变，只是重新建立一个session而已，所有的操作都在这个新的session中完成。具体细节见代码。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs js">#　<span class="hljs-variable constant_">MNIST</span>分类（续）<br><span class="hljs-title function_">print</span>(<span class="hljs-string">&quot;Starting 2nd session...&quot;</span>)<br><span class="hljs-keyword">with</span> tf.<span class="hljs-title class_">Session</span>() <span class="hljs-keyword">as</span> <span class="hljs-attr">sess</span>:<br>    # 初始化变量<br>    sess.<span class="hljs-title function_">run</span>(tf.<span class="hljs-title function_">global_variables_initializer</span>())<br>    # 恢复模型变量<br>    saver.<span class="hljs-title function_">restore</span>(sess, model_path)<br><br>    # 测试 model<br>    correct_prediction = tf.<span class="hljs-title function_">equal</span>(tf.<span class="hljs-title function_">argmax</span>(pred, <span class="hljs-number">1</span>), tf.<span class="hljs-title function_">argmax</span>(y, <span class="hljs-number">1</span>))<br>    # 计算准确率<br>    accuracy = tf.<span class="hljs-title function_">reduce_mean</span>(tf.<span class="hljs-title function_">cast</span>(correct_prediction, tf.<span class="hljs-property">float32</span>))<br>    print (<span class="hljs-string">&quot;Accuracy:&quot;</span>, accuracy.<span class="hljs-built_in">eval</span>(&#123;<br>    <span class="hljs-attr">x</span>: mnist.<span class="hljs-property">test</span>.<span class="hljs-property">images</span>, <span class="hljs-attr">y</span>: mnist.      test.<span class="hljs-property">labels</span>&#125;))<br><br>    output = tf.<span class="hljs-title function_">argmax</span>(pred, <span class="hljs-number">1</span>)<br>    batch_xs, batch_ys = mnist.<span class="hljs-property">train</span>.<span class="hljs-title function_">next_batch</span>(<span class="hljs-number">2</span>)<br>    outputval,predv = sess.<span class="hljs-title function_">run</span>([output,pred], feed_dict=&#123;<br>    <span class="hljs-attr">x</span>: batch_xs&#125;)<br>    <span class="hljs-title function_">print</span>(outputval,predv,batch_ys)<br>    im = batch_xs[<span class="hljs-number">0</span>]<br>    im = im.<span class="hljs-title function_">reshape</span>(-<span class="hljs-number">1</span>,<span class="hljs-number">28</span>)<br>    pylab.<span class="hljs-title function_">imshow</span>(im)<br>    pylab.<span class="hljs-title function_">show</span>()<br><br>    im = batch_xs[<span class="hljs-number">1</span>]<br>    im = im.<span class="hljs-title function_">reshape</span>(-<span class="hljs-number">1</span>,<span class="hljs-number">28</span>)<br>    pylab.<span class="hljs-title function_">imshow</span>(im)<br>    pylab.<span class="hljs-title function_">show</span>()<br></code></pre></td></tr></table></figure>

<p>以上代码可以替代原来的session，也可以直接放到代码后面，将前面的session注释掉。<br>输出结果<br><img src="https://gitee.com/fuyingyou/picgo/raw/master/img_algorithm/202403161546900.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<ul>
<li>第一行是模型的准确率，接下来是3个数组。</li>
<li>第一个数组是输出的预测结果[3,6]</li>
<li>第二个大的数组比较大，是预测出来的真实输出值，哪一项数值越大，代表对应的概率越大.</li>
<li>第三个大的数组元素都是0和1，是图片实际的标签值onehot编码表示的数字</li>
</ul>
<p>完整代码：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">import</span> tensorflow.<span class="hljs-property">compat</span>.<span class="hljs-property">v1</span> <span class="hljs-keyword">as</span> tf  # 导入tensorflow库#<br>tf.<span class="hljs-title function_">disable_v2_behavior</span>()<br><br><span class="hljs-keyword">from</span> tensorflow.<span class="hljs-property">examples</span>.<span class="hljs-property">tutorials</span>.<span class="hljs-property">mnist</span> <span class="hljs-keyword">import</span> input_data<br>mnist = input_data.<span class="hljs-title function_">read_data_sets</span>(<span class="hljs-string">&quot;MNIST_data/&quot;</span>, one_hot=<span class="hljs-title class_">True</span>)<br><br># print (<span class="hljs-string">&#x27;输入数据:&#x27;</span>,mnist.<span class="hljs-property">train</span>.<span class="hljs-property">images</span>)<br># print (<span class="hljs-string">&#x27;输入数据打印shape:&#x27;</span>,mnist.<span class="hljs-property">train</span>.<span class="hljs-property">images</span>.<span class="hljs-property">shape</span>)<br><br><span class="hljs-keyword">import</span> pylab<br>im = mnist.<span class="hljs-property">train</span>.<span class="hljs-property">images</span>[<span class="hljs-number">1</span>]<br>im = im.<span class="hljs-title function_">reshape</span>(-<span class="hljs-number">1</span>,<span class="hljs-number">28</span>)<br>pylab.<span class="hljs-title function_">imshow</span>(im)<br>pylab.<span class="hljs-title function_">show</span>()<br># print (<span class="hljs-string">&#x27;输入数据打印shape:&#x27;</span>,mnist.<span class="hljs-property">test</span>.<span class="hljs-property">images</span>.<span class="hljs-property">shape</span>)<br># print (<span class="hljs-string">&#x27;输入数据打印shape:&#x27;</span>,mnist.<span class="hljs-property">validation</span>.<span class="hljs-property">images</span>.<span class="hljs-property">shape</span>)<br><br>tf.<span class="hljs-title function_">reset_default_graph</span>()<br># 定义占位符<br>x = tf.<span class="hljs-title function_">placeholder</span>(tf.<span class="hljs-property">float32</span>, [<span class="hljs-title class_">None</span>, <span class="hljs-number">784</span>]) # <span class="hljs-variable constant_">MNIST</span>数据集的维度是  <span class="hljs-number">28</span>×<span class="hljs-number">28</span>=<span class="hljs-number">784</span><br>y = tf.<span class="hljs-title function_">placeholder</span>(tf.<span class="hljs-property">float32</span>, [<span class="hljs-title class_">None</span>, <span class="hljs-number">10</span>])  # 数字<span class="hljs-number">0</span>～<span class="hljs-number">9</span> ，共<span class="hljs-number">10</span>个类别<br>W = tf.<span class="hljs-title class_">Variable</span>(tf.<span class="hljs-title function_">random_normal</span>([<span class="hljs-number">784</span>,<span class="hljs-number">10</span>]))<br>b = tf.<span class="hljs-title class_">Variable</span>(tf.<span class="hljs-title function_">zeros</span>([<span class="hljs-number">10</span>]))<br>pred = tf.<span class="hljs-property">nn</span>.<span class="hljs-title function_">softmax</span>(tf.<span class="hljs-title function_">matmul</span>(x, W) + b) # <span class="hljs-title class_">Softmax</span>分类<br><br># 损失函数<br>cost=tf.<span class="hljs-title function_">reduce_mean</span>(-tf.<span class="hljs-title function_">reduce_sum</span>(y*tf.<span class="hljs-title function_">log</span>(pred),reduction_indices=<span class="hljs-number">1</span>))<br><br># 定义参数<br>learning_rate = <span class="hljs-number">0.01</span><br># 使用梯度下降优化器<br>optimizer=tf.<span class="hljs-property">train</span>.<span class="hljs-title class_">GradientDescentOptimizer</span>(learning_rate).<span class="hljs-title function_">minimize</span>(cost)<br>training_epochs = <span class="hljs-number">25</span><br>batch_size = <span class="hljs-number">100</span><br>display_step = <span class="hljs-number">1</span><br><br>saver = tf.<span class="hljs-property">train</span>.<span class="hljs-title class_">Saver</span>()<br>model_path = <span class="hljs-string">&quot;log/521model.ckpt&quot;</span><br><br># 启动session<br><span class="hljs-keyword">with</span> tf.<span class="hljs-title class_">Session</span>() <span class="hljs-keyword">as</span> <span class="hljs-attr">sess</span>:<br>    sess.<span class="hljs-title function_">run</span>(tf.<span class="hljs-title function_">global_variables_initializer</span>())# <span class="hljs-title class_">Initializing</span> <span class="hljs-variable constant_">OP</span><br>    # 启动循环开始训练<br>    <span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-title function_">range</span>(training_epochs):<br>        avg_cost = <span class="hljs-number">0.</span><br>        total_batch = <span class="hljs-title function_">int</span>(mnist.<span class="hljs-property">train</span>.<span class="hljs-property">num_examples</span>/batch_size)<br>        # 循环所有数据集<br>        <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-title function_">range</span>(total_batch):<br>            batch_xs, batch_ys = mnist.<span class="hljs-property">train</span>.<span class="hljs-title function_">next_batch</span>(batch_size)<br>            # 运行优化器<br>            _, c = sess.<span class="hljs-title function_">run</span>([optimizer, cost], feed_dict=&#123;<br>    <span class="hljs-attr">x</span>: batch_xs,<br>                                                       <span class="hljs-attr">y</span>: batch_ys&#125;)<br>            # 计算平均loss值<br>            avg_cost += c / total_batch<br>        # 显示训练中的详细信息<br>        <span class="hljs-keyword">if</span> (epoch+<span class="hljs-number">1</span>) % display_step == <span class="hljs-number">0</span>:<br>           print (<span class="hljs-string">&quot;Epoch:&quot;</span>, <span class="hljs-string">&#x27;%04d&#x27;</span> % (epoch+<span class="hljs-number">1</span>), <span class="hljs-string">&quot;cost=&quot;</span>, <span class="hljs-string">&quot;&#123;:.9f&#125;&quot;</span>.            <span class="hljs-title function_">format</span>(avg_cost))<br><br>    <span class="hljs-title function_">print</span>( <span class="hljs-string">&quot; Finished!&quot;</span>)<br>    # 测试 model<br>    correct_prediction = tf.<span class="hljs-title function_">equal</span>(tf.<span class="hljs-title function_">argmax</span>(pred, <span class="hljs-number">1</span>), tf.<span class="hljs-title function_">argmax</span>(y, <span class="hljs-number">1</span>))<br>    # 计算准确率<br>    accuracy = tf.<span class="hljs-title function_">reduce_mean</span>(tf.<span class="hljs-title function_">cast</span>(correct_prediction, tf.<span class="hljs-property">float32</span>))<br>    print (<span class="hljs-string">&quot;Accuracy:&quot;</span>, accuracy.<span class="hljs-built_in">eval</span>(&#123;<br>    <span class="hljs-attr">x</span>: mnist.<span class="hljs-property">test</span>.<span class="hljs-property">images</span>, <span class="hljs-attr">y</span>: mnist.<span class="hljs-property">test</span>.<span class="hljs-property">labels</span>&#125;))<br><br>    #     # 保存模型<br>    save_path = saver.<span class="hljs-title function_">save</span>(sess, model_path)<br>    <span class="hljs-title function_">print</span>(<span class="hljs-string">&quot;Model saved in file: %s&quot;</span> % save_path)<br><br><span class="hljs-title function_">print</span>(<span class="hljs-string">&quot;Starting 2nd session...&quot;</span>)<br><span class="hljs-keyword">with</span> tf.<span class="hljs-title class_">Session</span>() <span class="hljs-keyword">as</span> <span class="hljs-attr">sess</span>:<br>    # 初始化变量<br>    sess.<span class="hljs-title function_">run</span>(tf.<span class="hljs-title function_">global_variables_initializer</span>())<br>    # 恢复模型变量<br>    saver.<span class="hljs-title function_">restore</span>(sess, model_path)<br><br>    # 测试 model<br>    correct_prediction = tf.<span class="hljs-title function_">equal</span>(tf.<span class="hljs-title function_">argmax</span>(pred, <span class="hljs-number">1</span>), tf.<span class="hljs-title function_">argmax</span>(y, <span class="hljs-number">1</span>))<br>    # 计算准确率<br>    accuracy = tf.<span class="hljs-title function_">reduce_mean</span>(tf.<span class="hljs-title function_">cast</span>(correct_prediction, tf.<span class="hljs-property">float32</span>))<br>    print (<span class="hljs-string">&quot;Accuracy:&quot;</span>, accuracy.<span class="hljs-built_in">eval</span>(&#123;<br>    <span class="hljs-attr">x</span>: mnist.<span class="hljs-property">test</span>.<span class="hljs-property">images</span>, <span class="hljs-attr">y</span>: mnist.      test.<span class="hljs-property">labels</span>&#125;))<br><br>    output = tf.<span class="hljs-title function_">argmax</span>(pred, <span class="hljs-number">1</span>)<br>    batch_xs, batch_ys = mnist.<span class="hljs-property">train</span>.<span class="hljs-title function_">next_batch</span>(<span class="hljs-number">2</span>)<br>    outputval,predv = sess.<span class="hljs-title function_">run</span>([output,pred], feed_dict=&#123;<br>    <span class="hljs-attr">x</span>: batch_xs&#125;)<br>    <span class="hljs-title function_">print</span>(outputval,predv,batch_ys)<br>    im = batch_xs[<span class="hljs-number">0</span>]<br>    im = im.<span class="hljs-title function_">reshape</span>(-<span class="hljs-number">1</span>,<span class="hljs-number">28</span>)<br>    pylab.<span class="hljs-title function_">imshow</span>(im)<br>    pylab.<span class="hljs-title function_">show</span>()<br><br>    im = batch_xs[<span class="hljs-number">1</span>]<br>    im = im.<span class="hljs-title function_">reshape</span>(-<span class="hljs-number">1</span>,<span class="hljs-number">28</span>)<br>    pylab.<span class="hljs-title function_">imshow</span>(im)<br>    pylab.<span class="hljs-title function_">show</span>()<br></code></pre></td></tr></table></figure>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E5%85%B6%E5%AE%83/" class="category-chain-item">其它</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/" class="print-no-link">#神经网络</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>识别图中模糊的手写数字</div>
      <div>http://example.com/2020/11/29/课程实验/识别图中模糊的手写数字/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>fyy</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2020年11月29日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a class="print-no-link" target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/04/06/%E7%AE%97%E6%B3%95/LeetCode/LeetCode%20135.%20%E5%88%86%E5%8F%91%E7%B3%96%E6%9E%9C/" title="LeetCode 135. 分发糖果">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">LeetCode 135. 分发糖果</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2020/11/01/%E7%AE%97%E6%B3%95/LeetCode/LeetCode%20463.%20%E5%B2%9B%E5%B1%BF%E7%9A%84%E5%91%A8%E9%95%BF/" title="LeetCode 463. 岛屿的周长">
                        <span class="hidden-mobile">LeetCode 463. 岛屿的周长</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments" lazyload>
      
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://lib.baomitu.com/valine/1.5.1/Valine.min.js', function() {
        var options = Object.assign(
          {"appId":"ufph8TbmK43d1JfQxLvss4KY-MdYXbMMI","appKey":"96APiPOtZEiCQlv5h3jjZfbC","path":"window.location.pathname","placeholder":null,"avatar":"retro","meta":["nick","mail","link"],"requiredFields":[],"pageSize":10,"lang":"zh-CN","highlight":false,"recordIP":false,"serverURLs":"","emojiCDN":null,"emojiMaps":null,"enableQQ":false},
          {
            el: "#valine",
            path: window.location.pathname
          }
        )
        new Valine(options);
        Fluid.utils.waitElementVisible('#valine .vcontent', () => {
          var imgSelector = '#valine .vcontent img:not(.vemoji)';
          Fluid.plugins.imageCaption(imgSelector);
          Fluid.plugins.fancyBox(imgSelector);
        })
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="leancloud-site-pv-container" style="display: none">
        总访问量
        <span id="leancloud-site-pv"></span>
        次
      </span>
    
    
      <span id="leancloud-site-uv-container" style="display: none">
        总访客数
        <span id="leancloud-site-uv"></span>
        人
      </span>
    
    

  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.4/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.20.1/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script defer src="/js/leancloud.js" ></script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
